package conversation

import (
	"coze_for_sdk/controller"
	"coze_for_sdk/model"
	"time"

	"github.com/gin-gonic/gin"
)

func Delete(c *gin.Context) {

	conversationId := c.Query("conversation_id")
	userId := c.GetInt("user_id")

	if conversationId == "" {
		controller.Fail(c, controller.ParamsError, "会话ID不能为空")
		return
	}

	// 更新会话状态
	db := model.GetInstance().GetDB()

	var conversation model.CozeConversation
	conversation, err := conversation.GetConversation(conversationId, userId)
	if err != nil {
		controller.Fail(c, controller.BusinessError, "会话不存在")
		return
	}

	// 更新会话状态
	db.Model(&model.CozeConversation{}).
		Where("conversation_id = ?", conversationId).
		Where("user_id = ?", userId).
		Updates(map[string]interface{}{
			"status":     model.CozeConversationStatusEnd,
			"updated_at": time.Now(),
		})

	// 更新消息状态
	db.Model(&model.CozeChat{}).
		Where("conversation_id = ?", conversationId).
		Where("user_id = ?", userId).
		Updates(map[string]interface{}{
			"status":     model.CozeChatStatusEnd,
			"updated_at": time.Now(),
		})

	controller.Success(c, "")
}
